import os
from pathlib import Path

def create_file(file_path, content):
    """Create a file with the given content"""
    os.makedirs(os.path.dirname(file_path), exist_ok=True)
    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(content)

def fix_android_resources(project_root):
    """Fix missing resources in the Android project"""
    # Define paths
    res_dir = os.path.join(project_root, 'app', 'src', 'main', 'res')
    values_dir = os.path.join(res_dir, 'values')
    drawable_dir = os.path.join(res_dir, 'drawable')
    layout_dir = os.path.join(res_dir, 'layout')
    
    # 1. Update colors.xml
    colors_content = """<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
    
    <color name="background">#F5F5F5</color>
    <color name="alarmBackground">#FFF9C4</color>
    <color name="textPrimary">#212121</color>
    <color name="textSecondary">#757575</color>
</resources>"""
    create_file(os.path.join(values_dir, 'colors.xml'), colors_content)
    
    # 2. Create bg_button.xml
    bg_button_content = """<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/colorPrimary"/>
    <corners android:radius="8dp"/>
</shape>"""
    create_file(os.path.join(drawable_dir, 'bg_button.xml'), bg_button_content)
    
    # 3. Create bg_card.xml
    bg_card_content = """<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@android:color/white"/>
    <corners android:radius="8dp"/>
</shape>"""
    create_file(os.path.join(drawable_dir, 'bg_card.xml'), bg_card_content)
    
    # 4. Update activity_alarm_alert.xml
    alarm_alert_content = """<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:padding="32dp"
    android:background="@color/alarmBackground">
    
    <TextView
        android:id="@+id/tv_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:textColor="@android:color/black"
        android:text="闹钟时间到了"
        android:layout_marginBottom="32dp"/>
    
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        
        <Button
            android:id="@+id/btn_dismiss"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="关闭"
            android:padding="16dp"
            android:textSize="18sp"
            android:background="@drawable/bg_button"
            android:textColor="@android:color/white"/>
        
        <Button
            android:id="@+id/btn_snooze"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="32dp"
            android:text="小睡"
            android:padding="16dp"
            android:textSize="18sp"
            android:background="@drawable/bg_button"
            android:textColor="@android:color/white"/>
    </LinearLayout>
</LinearLayout>"""
    create_file(os.path.join(layout_dir, 'activity_alarm_alert.xml'), alarm_alert_content)
    
    print("✅ Successfully fixed missing resources in the Android project!")

if __name__ == "__main__":
    # Get the project root directory (where this script is located)
    project_root = input("Enter the path to your Android project root directory: ").strip()
    
    # Validate path
    if not os.path.isdir(project_root):
        print(f"Error: The path '{project_root}' is not a valid directory.")
    elif not os.path.exists(os.path.join(project_root, 'app', 'src', 'main')):
        print("Error: This doesn't appear to be an Android project directory.")
        print("Please provide the root directory of your Android project (contains app/, gradle/, etc.)")
    else:
        fix_android_resources(project_root)
        print("\nNext steps:")
        print("1. In Android Studio, go to Build -> Clean Project")
        print("2. Then Build -> Rebuild Project")
        print("3. If issues persist, File -> Invalidate Caches / Restart")